﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace KingOfCSharp.iHunter.Plugins.RssReader
{
    public partial class RssConfigDialog : UserControl, System.Xml.Serialization.IXmlSerializable
    {
        private XmlDocument settings;
        private BackgroundWorker bw;
        private bool testPassed = false;
        private string title;

        public RssConfigDialog()
        {
            InitializeComponent();

            settings = new XmlDocument();
            settings.LoadXml("<RSS><Uri/></RSS>");

            bw = new BackgroundWorker();
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            settings.Load(reader);
            textBoxUri.Text = settings.DocumentElement["Uri"].InnerText;
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            if (settings.DocumentElement["Uri"].InnerText == string.Empty)
                throw new ArgumentException("Please input correct address and pass the test.");

            settings.WriteTo(writer);
        }

        #endregion

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader sr = null;
            XmlDocument xml = null;

            try
            {
                request = (HttpWebRequest)WebRequest.Create(textBoxUri.Text);
                request.Method = "GET";
                response = (HttpWebResponse)request.GetResponse();
                sr = new StreamReader(response.GetResponseStream());
                xml = new XmlDocument();
                xml.LoadXml(sr.ReadToEnd());

                title = xml.DocumentElement["channel"]["title"].InnerText;
                testPassed = true;

            }
            catch
            {
                testPassed = false;
            }
        }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (testPassed)
            {
                textBoxDisplay.Text = "Test Successfully for:\n" + title;
                settings.DocumentElement["Uri"].InnerText = textBoxUri.Text;
            }
            else
            {
                textBoxDisplay.Text = "Can't get the information of the RSS source.";
            }
        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            if (bw.IsBusy != true)
            {
                textBoxDisplay.Text = "Testing... Please wait a moment.";
                bw.RunWorkerAsync();
            }
        }
    }
}
